package com.woniuxy.userapp.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.authapp.client.AuthClient;
import com.woniuxy.authapp.client.param.Oauth2TokenInfo;
import com.woniuxy.commons.util.Result;
import com.woniuxy.commons.util.WebUtil;
import com.woniuxy.userapp.common.exception.Myexception.UserException;
import com.woniuxy.userapp.common.exception.code.UserExceptionCode;
import com.woniuxy.userapp.infra.model.mapper.BackgroundAccountMapper;
import com.woniuxy.userapp.infra.model.mapper.model.BackgroundAccount;
import com.woniuxy.userapp.infra.model.mapper.model.Role;
import com.woniuxy.userapp.infra.model.mapper.model.dto.GetRoleListDto;
import com.woniuxy.userapp.service.BackgroundAccountService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.userapp.service.constant.AuthConstant;
import com.woniuxy.userapp.service.dto.UserDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.HttpRequestMethodNotSupportedException;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 
 * @since 2022-08-03
 */
@Service
public class BackgroundAccountServiceImpl extends ServiceImpl<BackgroundAccountMapper, BackgroundAccount> implements BackgroundAccountService {

    @Override
    public UserDto loadUserByUsername(String userName) {
        //根据用户名获取当前用户对象
        BackgroundAccount account = baseMapper.selectOne(new QueryWrapper<BackgroundAccount>().eq("user_name", userName));
        //前置判断
        if(account == null) throw new UserException(UserExceptionCode.ACCOUNT_NOT_EXISTS);
        //有此对象
        //根据对象获取其角色集合
        List<GetRoleListDto> roleList = getRoleList(account.getId());
        if(roleList.size() <=0 || roleList == null) throw new UserException(UserExceptionCode.ACCOUNT_NOT_ROLE);
        UserDto userDto = new UserDto();
        BeanUtil.copyProperties(account,userDto);
        //将权限集合放入userdto中
        userDto.setClientId("admin-app");
        List<String> stringStream = roleList.stream().map(item -> item.getId() + "_" + item.getRoleName()).collect(Collectors.toList());
        userDto.setRoles(stringStream);
        return userDto;
    }

    //根据id获取角色权限集合
    @Override
    public List<GetRoleListDto> getRoleList(Integer id) {
        List<GetRoleListDto> roleList = baseMapper.getRoleList(id);
        return roleList;
    }


    @Autowired
    private AuthClient authClient;
    /**
     * 登录
     * @param username
     * @param password
     */
    @Override
    public Result<Oauth2TokenInfo> login(String username, String password) throws HttpRequestMethodNotSupportedException {
        if(StrUtil.isEmpty(username) || StrUtil.isEmpty(password)) throw new UserException(UserExceptionCode.USERNAME_OR_PASSWORD_NULL);
        Map<String, String> params = new HashMap<>();
        params.put("client_id", AuthConstant.ADMIN_CLIENT_ID);
        params.put("client_secret","123456");
        params.put("grant_type","password");
        params.put("username",username);
        params.put("password",password);
        Result<Oauth2TokenInfo> result = authClient.postAccessToken("password", AuthConstant.ADMIN_CLIENT_ID, "123456", null, username, password);
        return result;
    }
}
